// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-08

package nod

import (
	"context"
	"integration/app/lib/conf"
	"integration/app/lib/nod/customer"
	"integration/app/lib/nod/sell_order"
	"integration/app/lib/nod/sell_refund"
	"integration/app/models"
	"sync"
)

var (
	// Builder
	// 是一个单例, 用于构建发起API请求时的入参数据.
	//
	//   nod.Builder.SellOrder(ctx, order, carts)
	Builder ErpBuilder

	once = new(sync.Once)
)

type (
	// ErpBuilder
	// 构建入参数据.
	ErpBuilder interface {
		// Customer
		// 基于CRMEB订单构建ERP客户资料.
		Customer(ctx context.Context, agent *conf.ErpAgent, order *models.Order) (customer *customer.Entity, err error)

		// SellOrder
		// 基于CRMEB订单构建ERP销售订单.
		//
		//   nod.Builder.SellOrder(ctx, order, carts)
		SellOrder(ctx context.Context, agent *conf.ErpAgent, order *models.Order, carts []*models.OrderCartInfo) (*sell_order.Entity, error)

		// SellRefund
		// 基于CRMEB订单构建ERP销售退单.
		//
		//   nod.Builder.SellOrder(ctx, order, carts)
		SellRefund(ctx context.Context, agent *conf.ErpAgent, order *models.Order, carts []*models.OrderCartInfo) (res *sell_refund.Entity, err error)
	}

	eb struct{}
)

// Customer
// 基于CRMEB订单构建ERP客户资料.
func (o *eb) Customer(ctx context.Context, agent *conf.ErpAgent, order *models.Order) (res *customer.Entity, err error) {
	res = customer.New()
	err = res.Parse(ctx, agent, order)
	return
}

// SellOrder
// 基于CRMEB订单构建ERP销售订单.
func (o *eb) SellOrder(ctx context.Context, agent *conf.ErpAgent, order *models.Order, carts []*models.OrderCartInfo) (res *sell_order.Entity, err error) {
	res = sell_order.New()
	err = res.Parse(ctx, agent, order, carts)
	return
}

// SellRefund
// 基于CRMEB订单构建ERP销售退单.
func (o *eb) SellRefund(ctx context.Context, agent *conf.ErpAgent, order *models.Order, carts []*models.OrderCartInfo) (res *sell_refund.Entity, err error) {
	res = sell_refund.New()
	err = res.Parse(ctx, agent, order, carts)
	return
}

func (o *eb) init() *eb { return o }
